package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Goods;
import cdu.javaendwork.model.sqlModel.Items;
import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.Shopping.GoodsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.ItemsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.OrderServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.GoodsService;
import cdu.javaendwork.service.Interface.Shopping.ItemsService;
import cdu.javaendwork.service.Interface.Shopping.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

@WebServlet("/add_items")
public class AddItemsController extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    OrderService orderService = new OrderServiceImpl();
    ItemsService itemsService = new ItemsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gid=req.getParameter("goodid");
        int goodid = Integer.parseInt(gid);
        String sPage=req.getParameter("page");
        int page=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);


        Goods goods = goodsService.findById(goodid);

        req.setAttribute("page",page);
        req.setAttribute("goods" , goods);

        req.getRequestDispatcher("addItems.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理参数
        HttpSession session = req.getSession();
        User user= (User)session.getAttribute("user");
        int userid = user.getId();

        String gid=req.getParameter("goodid");
        int goodid = Integer.parseInt(gid);
        String gcnt=req.getParameter("good_cnt");
        int goods_cnt = Integer.parseInt(gcnt);
        String p1=req.getParameter("page");
        int page = Integer.parseInt(p1);
        String p=req.getParameter("price");

        BigDecimal price = new BigDecimal(p);
        //计算购物车该条目总价格
        BigDecimal sumPrice = price.multiply(new BigDecimal(goods_cnt));
        //寻找一下该用户是否有存在的购物车
        Order oldCraft = orderService.findByUserIdAndStatus(userid,1);
        System.out.println(oldCraft);
        //如果没有则创建一个新的购物车
        if(oldCraft == null) orderService.insert(userid ,1,new Timestamp(new Date().getTime()),new BigDecimal(0)," ");
        //找到该用户的购物车id
        Order realCraft = orderService.findByUserIdAndStatus(userid , 1);
        int orderId = realCraft.getId();
        BigDecimal craftPrice = realCraft.getPrice().add(sumPrice);
        //寻找有没有相同商品的条目
        Items oldItems = itemsService.findSameItems(goodid,orderId);
        //如果存在的话 将价格和数量记录下来并且删除原条目
        BigDecimal oldItemsPrice = new BigDecimal(0);
        int oldCnt = 0;
        if(oldItems != null)
        {
            oldCnt = oldItems.getGood_cnt();
            oldItemsPrice = oldItems.getPrice();
            itemsService.delete(oldItems.getId());
        }
        //添加新条目到购物车中
        itemsService.insert(orderId,goodid,goods_cnt+oldCnt,sumPrice.add(oldItemsPrice));

        //修改原购物车总价格
        orderService.update(orderId,userid ,1,new Timestamp(new Date().getTime()),craftPrice," ");

        //跳转回超市界面
        resp.sendRedirect("market?page="+page);
    }
}
